package app.android.com.wuandroidapp.sample;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import app.android.com.utilscode.util.ToastUtils;
import app.android.com.wuandroidapp.R;
import app.android.com.wuandroidapp.library.activity.BaseNoTitleActivity;
import app.android.com.wuandroidapp.library.view.HeaderLayoutManagerFixed;
import app.android.com.wuandroidapp.library.view.ParallaxRecyclerAdapter;
import app.android.com.wuandroidapp.test.activity.MainActivity;

public class PersonActivity extends BaseNoTitleActivity implements ParallaxRecyclerAdapter.OnParallaxScroll {
    private boolean isNormalAdapter = false;
    private RecyclerView mRecyclerView;
    RelativeLayout mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler);
        mToolbar = (RelativeLayout) findViewById(R.id.title1);
        createAdapter(mRecyclerView);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
//        if (isNormalAdapter) {
//            createCardAdapter(mRecyclerView);
//        } else {
        createAdapter(mRecyclerView);
        //   }
        isNormalAdapter = !isNormalAdapter;
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    private void createCardAdapter(RecyclerView recyclerView) {
        final List<String> content = new ArrayList<>();
//        for (int i = 0; i < 50; i++) {
//            content.add("item " + i);
//        }
        content.add("我的钱包");
        content.add("我的优惠");
        content.add("我的贴纸");
        content.add("我的行程");
        content.add("用户指南");
        content.add("设置");

        final ParallaxRecyclerAdapter<String> adapter = new ParallaxRecyclerAdapter<String>(content) {
            @Override
            public void onBindViewHolderImpl(RecyclerView.ViewHolder viewHolder, ParallaxRecyclerAdapter<String> adapter, int i) {
                ((ViewHolder) viewHolder).textView.setText(adapter.getData().get(i));
            }

            @Override
            public RecyclerView.ViewHolder onCreateViewHolderImpl(ViewGroup viewGroup, final ParallaxRecyclerAdapter<String> adapter, int i) {
                return new ViewHolder(getLayoutInflater().inflate(R.layout.row_recyclerview_cards, viewGroup, false));
            }

            @Override
            public int getItemCountImpl(ParallaxRecyclerAdapter<String> adapter) {
                return content.size();
            }
        };

        adapter.setOnClickEvent(new ParallaxRecyclerAdapter.OnClickEvent() {
            @Override
            public void onClick(View v, int position) {
                Toast.makeText(PersonActivity.this, "You clicked '" + position + "'", Toast.LENGTH_SHORT).show();
            }
        });

        HeaderLayoutManagerFixed layoutManagerFixed = new HeaderLayoutManagerFixed(this);
        recyclerView.setLayoutManager(layoutManagerFixed);
        View header = getLayoutInflater().inflate(R.layout.header, recyclerView, false);
        layoutManagerFixed.setHeaderIncrementFixer(header);
        adapter.setShouldClipView(false);
        adapter.setOnParallaxScroll(this);
        adapter.setParallaxHeader(header, recyclerView);
        adapter.setData(content);
        recyclerView.setAdapter(adapter);


    }


    private void createAdapter(RecyclerView recyclerView) {
        final List<String> content = new ArrayList<>();
     /*   for (int i = 0; i < 50; i++) {
            content.add("item " + i);
        }*/

        content.add("我的钱包");
        content.add("我的优惠");
        content.add("我的贴纸");
        content.add("我的行程");
        content.add("我的消息");
        content.add("邀请信息");
        content.add("用户指南");
        content.add("设置");
        final ParallaxRecyclerAdapter<String> adapter = new ParallaxRecyclerAdapter<String>(content) {

            @Override
            public void onBindViewHolderImpl(RecyclerView.ViewHolder viewHolder, ParallaxRecyclerAdapter<String> adapter, int i) {
                ((ViewHolder) viewHolder).textView.setText(adapter.getData().get(i));
            }

            @Override
            public RecyclerView.ViewHolder onCreateViewHolderImpl(ViewGroup viewGroup, final ParallaxRecyclerAdapter<String> adapter, int i) {
                      return new ViewHolder(getLayoutInflater().inflate(R.layout.row_recyclerview, viewGroup, false));
            }

            @Override
            public int getItemCountImpl(ParallaxRecyclerAdapter<String> adapter) {
                return content.size();
            }

        };


        adapter.setOnClickEvent(new ParallaxRecyclerAdapter.OnClickEvent() {
            @Override
            public void onClick(View v, int position) {
                if(position==0){
                    startActivity(new Intent(PersonActivity.this,MyWalleyActivity.class));
                }
                Toast.makeText(PersonActivity.this, "You clicked '" + position + "'", Toast.LENGTH_SHORT).show();
            }
        });


        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        View header = getLayoutInflater().inflate(R.layout.header, recyclerView, false);
        adapter.setOnParallaxScroll(this);
        adapter.setParallaxHeader(header, recyclerView);
        adapter.setData(content);
        recyclerView.setAdapter(adapter);
    }


    @Override
    public void onParallaxScroll(float percentage, float offset, View parallax) {

       Log.i("percentage:",percentage+"");
        Log.i("offset:",offset+"");
//        Drawable c = mToolbar.getBackground();
//        c.setAlpha(Math.round(percentage * 255));
//        mToolbar.setBackground(c);

    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView textView;

        public ViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.textView);
        }
    }

    public void back(View v) {
        finish();

    }

}
